package com.ecoolex.ying.space.admin.graphql;

import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.pojo.Sorting;
import com.ecoolex.framework.common.util.Assert;
import com.ecoolex.framework.common.util.Check;
import com.ecoolex.framework.modularize.api.auth.admin.AdminMemberAuthority;
import com.ecoolex.framework.modularize.api.auth.annotation.AdminAuthoritativeRegister;
import com.ecoolex.framework.modularize.api.service.storage.*;
import com.ecoolex.framework.web.api.graphql2.annotation.*;
import com.ecoolex.ying.space.admin.core.AdminConstant;
import com.ecoolex.ying.space.entity.GoodsBrand;
import com.ecoolex.ying.space.pojo.AddGoodsBrandDTO;
import com.ecoolex.ying.space.pojo.EditGoodsBrandDTO;
import com.ecoolex.ying.space.pojo.QueryGoodsBrandDTO;
import com.ecoolex.ying.space.ws.GoodsService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.NonNull;

import static com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module.Type.Admin;
import static com.ecoolex.ying.space.admin.graphql.GoodsBrandGraphql.AUTH;

@GraphQL2Module(type = Admin, description = "商品品牌管理")
@AdminAuthoritativeRegister(parentKey = AdminConstant.AUTH_MODULE_KEY, key = AUTH, name = "商品品牌管理")
public class GoodsBrandGraphql {
    static final String AUTH = AdminConstant.AUTH_MODULE_KEY + "-GOODS-BRAND";
    static final String AUTH_GET = AUTH + "-GET";
    static final String AUTH_QUERY = AUTH + "-QUERY";
    static final String AUTH_ADD = AUTH + "-ADD";
    static final String AUTH_EDIT = AUTH + "-EDIT";
    static final String AUTH_DEL = AUTH + "-DEL";

    static final UploadSource IMAGE_UPLOAD_SOURCE = new UploadSource("GoodsBrand", "Image");
    static final UploadLimit IMAGE_UPLOAD_LIMIT = new UploadLimit(FileSize.MB.byteLength(10), MimeType.IMAGE.commons());

    static final UploadSource LOGO_UPLOAD_SOURCE = new UploadSource("GoodsBrand", "Logo");
    static final UploadLimit LOGO_UPLOAD_LIMIT = new UploadLimit(FileSize.MB.byteLength(10), MimeType.IMAGE.commons());

    @Reference
    private GoodsService goodsService;
    @Autowired
    private StorageService storageService;

    @TypeField(type = GoodsBrand.class, field = "canEdit", description = "能否编辑")
    public boolean canEdit(@Source GoodsBrand source, AdminMemberAuthority user) {
        return user.auth(AUTH_EDIT);
    }

    @TypeField(type = GoodsBrand.class, field = "canDel", description = "能否删除")
    public boolean canDel(@Source GoodsBrand source, AdminMemberAuthority user) {
        return user.auth(AUTH_DEL);
    }

    @TypeField(type = GoodsBrand.class, field = "image", description = "图片")
    public StorageInfo image(@Source GoodsBrand source) {
        return storageService.get(source.getImage());
    }

    @TypeField(type = GoodsBrand.class, field = "logo", description = "图片")
    public StorageInfo logo(@Source GoodsBrand source) {
        return storageService.get(source.getLogo());
    }

    @TypeField(type = GoodsBrand.class, field = "usedGoodsNumber", description = "使用品牌的商品数量")
    public long usedGoodsNumber(@Source GoodsBrand source) {
        return goodsService.countGoodsByBrandId(source.getBrandId());
    }

    @QueryField(field = "goodsBrand", description = "获取商品品牌详情")
    @AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_GET, name = "获取商品品牌详情")
    public GoodsBrand get(@NonNull Integer brandId, AdminMemberAuthority user) {
        user.assertAuth(AUTH_GET);
        return goodsService.getBrandById(brandId);
    }

    @QueryField(field = "goodsBrandList", description = "查询商品品牌列表")
    @AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_QUERY, name = "查询商品品牌列表")
    public Pagination<GoodsBrand> queryBrand(@NonNull QueryGoodsBrandDTO query, @NonNull Sorting[] sorts, @NonNull Page page, AdminMemberAuthority user) {
        user.assertAuth(AUTH_QUERY);
        return goodsService.queryBrand(query, sorts, page);
    }

    @MutationField(field = "uploadGoodsBrandImage", description = "上传商品品牌图片")
    public StorageInfo updateImage(@NonNull Upload request, AdminMemberAuthority user) {
        if (user.auth(AUTH_ADD) || user.auth(AUTH_EDIT)) {
            return storageService.upload(request, IMAGE_UPLOAD_SOURCE, IMAGE_UPLOAD_LIMIT);
        } else {
            return user.notAuthority();
        }
    }

    @MutationField(field = "uploadGoodsBrandLogo", description = "上传商品品牌Logo")
    public StorageInfo updateLogo(@NonNull Upload request, AdminMemberAuthority user) {
        if (user.auth(AUTH_ADD) || user.auth(AUTH_EDIT)) {
            return storageService.upload(request, LOGO_UPLOAD_SOURCE, LOGO_UPLOAD_LIMIT);
        } else {
            return user.notAuthority();
        }
    }

    @MutationField(field = "addGoodsBrand", description = "添加商品品牌")
    @AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_ADD, name = "添加商品品牌")
    public GoodsBrand add(@NonNull AddGoodsBrandDTO add, AdminMemberAuthority user) {
        user.assertAuth(AUTH_ADD);
        return goodsService.createBrand(add, user.userName());
    }

    @MutationField(field = "editGoodsBrand", description = "编辑商品品牌")
    @AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_EDIT, name = "编辑商品品牌")
    public boolean edit(@NonNull EditGoodsBrandDTO edit, AdminMemberAuthority user) {
        user.assertAuth(AUTH_EDIT);
        goodsService.updateBrand(edit, user.userName());
        return true;
    }

    @MutationField(field = "delGoodsBrand", description = "删除商品品牌")
    @AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_DEL, name = "删除商品品牌")
    public boolean del(@NonNull Integer brandId, AdminMemberAuthority user) {
        GoodsBrand record = goodsService.getBrandById(brandId);
        Assert.isTrue(Check.notNull(record), "商品品牌不存在");
        goodsService.deleteBrand(record, user.userName());
        return true;
    }

}
